//
//  ComingMovieViewController.m
//  A段项目
//
//  Created by ～whc on 15-6-17.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ComingMovieViewController.h"
#define tableHeight 200
@interface ComingMovieViewController ()

@end

@implementation ComingMovieViewController

- (void)dealloc
{
    [self.ComingMovieTable release];
    [self.array release];
    [self.scrollView release];
    [self.mainScrollView release];
    [self.ArrayId release];
    [self.HUD release];
    [self.movieDetail release];
    [self.titlabel release];
    [self.titleLabel release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];

    self.navigationController.navigationBar.translucent = NO;
    self.array = [NSMutableArray array];
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中";
    [self.HUD show:YES];

    
    [self afnGetRequest];
    
    
}

-(void)headerRefesh
{
    self.ComingMovieTable.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading = NO;
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载中";
        [self.HUD show:YES];
        [self afnGetRequest];
        
        [self.ComingMovieTable reloadData];
        [self.HUD hide:YES];
        // 拿到当前的下拉刷新控件，结束刷新状态
        [self.ComingMovieTable.header endRefreshing];
        
    }];
    
    
}


- (void)createTable5
{
    
    self.ComingMovieTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64 - 49) style:UITableViewStylePlain];
    self.ComingMovieTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    self.ComingMovieTable.scrollEnabled = NO;
    //    [self.ComingMovieTable setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:self.ComingMovieTable];
    self.ComingMovieTable.delegate = self;
    self.ComingMovieTable.dataSource = self;
    
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    [self.view addSubview:headerView];
    
    self.ComingMovieTable.tableHeaderView = headerView;
    
    
    self.scrollView = [[ScrollView alloc]initWithFrame:CGRectMake(0, 10, WIDTH, 230)];
    //    self.scrollView.delegate = self;
    //    self.scrollView.showsHorizontalScrollIndicator = NO;
    //    self.scrollView.contentSize = CGSizeMake(360*7, 0);
    [headerView addSubview:self.scrollView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
    titleLabel.text = @"最受关注";
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [headerView addSubview:titleLabel];
    [titleLabel release];
    
    
    
    UILabel *titlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.scrollView.frame.origin.y+self.scrollView.frame.size.height+20, 100, 30)];
    titlabel.text = @"即将上映";
    titlabel.font = [UIFont boldSystemFontOfSize:20];
    [headerView addSubview:titlabel];
    [titlabel release];
    
}



- (void)createTable6
{
    
    self.ComingMovieTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64 - 49) style:UITableViewStylePlain];
//    self.ComingMovieTable.scrollEnabled = NO;
    //    [self.ComingMovieTable setBackgroundColor:[UIColor redColor]];
    self.ComingMovieTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.ComingMovieTable];
    self.ComingMovieTable.delegate = self;
    self.ComingMovieTable.dataSource = self;

    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    [self.view addSubview:headerView];
    
    self.ComingMovieTable.tableHeaderView = headerView;
    
    
    self.scrollView = [[ScrollView alloc]initWithFrame:CGRectMake(0, 10, WIDTH, 230)];
    //    self.scrollView.delegate = self;
    //    self.scrollView.showsHorizontalScrollIndicator = NO;
    //    self.scrollView.contentSize = CGSizeMake(360*7, 0);
    [headerView addSubview:self.scrollView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
    titleLabel.text = @"最受关注";
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [headerView addSubview:titleLabel];
    [titleLabel release];
    
    
    
    UILabel *titlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.scrollView.frame.origin.y+self.scrollView.frame.size.height+20, 100, 30)];
    titlabel.text = @"即将上映";
    titlabel.font = [UIFont boldSystemFontOfSize:20];
    [headerView addSubview:titlabel];
    [titlabel release];
    
}



- (void)createTablePlus
{
    
    self.ComingMovieTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64 - 49) style:UITableViewStylePlain];
    self.ComingMovieTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    self.ComingMovieTable.scrollEnabled = NO;
    //    [self.ComingMovieTable setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:self.ComingMovieTable];
    self.ComingMovieTable.delegate = self;
    self.ComingMovieTable.dataSource = self;
    
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    [self.view addSubview:headerView];
    
    self.ComingMovieTable.tableHeaderView = headerView;
    
    
    self.scrollView = [[ScrollView alloc]initWithFrame:CGRectMake(0, 10, WIDTH, 230)];
    //    self.scrollView.delegate = self;
    //    self.scrollView.showsHorizontalScrollIndicator = NO;
    //    self.scrollView.contentSize = CGSizeMake(360*7, 0);
    [headerView addSubview:self.scrollView];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
    titleLabel.text = @"最受关注";
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [headerView addSubview:titleLabel];
    [titleLabel release];
    
    
    
    UILabel *titlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.scrollView.frame.origin.y+self.scrollView.frame.size.height+20, 100, 30)];
    titlabel.text = @"即将上映";
    titlabel.font = [UIFont boldSystemFontOfSize:20];
    [headerView addSubview:titlabel];
    [titlabel release];
    
}


#warning 创建即将上映的tableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return tableHeight;
}
#warning 即将上映的tableView必须实现的方法


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"comingMovieCell";
    ComingMovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[ComingMovieTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    Coming *mov = [self.array objectAtIndex:indexPath.row];
      cell.com = mov;
    return cell;
    
}



#warning 实现点击进入详情页面事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.ComingMovieTable deselectRowAtIndexPath:indexPath animated:YES];
    self.movieDetail = [[MovieDetailViewController alloc] init];
    self.movieDetail.hidesBottomBarWhenPushed = YES;
    Coming *mov = [self.array objectAtIndex:indexPath.row];
    self.movieDetail.movieID = mov.movId;
    [self.navigationController pushViewController:self.movieDetail animated:YES];
    
    [self.movieDetail release];
    
    NSLog(@"%ld",self.movId);
}


-(void)afnGetRequest
{
   
    self.array = [[DateHandel shareHandle]selectAllComingMovie];
    
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *url_string = @"http://api.m.mtime.cn/Movie/MovieComingNew.api?locationId=729";
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"application/x-javascript",@"text/html",nil];
    
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
//                    NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        [[DateHandel shareHandle]dropComingMovieTable];
        [[DateHandel shareHandle]createComingTable];
        [self.array removeAllObjects];
        
        NSMutableArray *array = [NSMutableArray array];
        array = [responseObject objectForKey:@"moviecomings"];
        
        self.ArrayId = [NSMutableArray array];
        for (NSMutableDictionary *dic in array) {
            Coming *com = [[Coming alloc]init];
            com.title = [dic objectForKey:@"title"];
            com.image = [dic objectForKey:@"image"];
            com.releaseDate = [dic objectForKey:@"releaseDate"];
            com.director = [dic objectForKey:@"director"];
            com.actor1 = [dic objectForKey:@"actor1"];
            com.actor2 = [dic objectForKey:@"actor2"];
            com.type = [dic objectForKey:@"type"];
//            NSLog(@"type = %@", com.type);
            com.wantedCount = [[dic objectForKey:@"wantedCount"]integerValue];
            com.videoId = [[dic objectForKey:@"videoCount"]integerValue];
            
            com.movId = [[dic objectForKey:@"id"]integerValue];
            [self.array addObject:com];
            [self.ArrayId addObject:[NSString stringWithFormat:@"%ld",com.movId]];
            [[DateHandel shareHandle]insertcomingMovie:com];
            [com release];

        }
        
        [self.ComingMovieTable reloadData];
        if (WIDTH == 320 && HEIGHT == 568) {
            [self createTable5];
        }
        if (WIDTH == 414 && HEIGHT == 736) {
            [self createTablePlus];
        }
        if (WIDTH == 375 && HEIGHT == 667){
            [self createTable6];
        }
        [self headerRefesh];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
                [self.HUD hide:YES];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert autorelease];
//        [self createTable6];
        
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
